package com.eudon.ai.agent.app;

import com.eudon.ai.agent.advisor.LoggerAdvisor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author eudon
 * @description 找到有缘人
 * @date 2025/8/12
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
@Slf4j
@Component
public class FindLove {
    private final String SYSTEM_PROMPT = "你是一个专职于牵红线的介绍人，当用户给出自己的信息和需求时，你会帮助用户找到对应符合要求的潜在心仪对象，并且按照一条条的格式返回给用户";
    private final ChatClient chatClient;
    private final VectorStore vectorStore;

    FindLove(ChatModel dashscopeChatModel, VectorStore myPostgresVectorStore) {
        this.chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(new LoggerAdvisor(),
                        new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
                .build();
        this.vectorStore = myPostgresVectorStore;
    }

    public String findLove(String userPrompt, String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(userPrompt)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new QuestionAnswerAdvisor(vectorStore))
                .call()
                .chatResponse();
        return response.getResult().getOutput().getText();
    }
}
